<template>
    <view class="content">
        <view class="header" :style="{height: navHeight+navTop+6+'px',paddingTop: navTop+'px'}">
            <view class="header-box u-flex u-row-center" :style="{height: navHeight+'px',lineHeight:navHeight+'px'}">
                <view class="header-logo">购物车（{{list.length || 0}}）</view>
            </view>
        </view>
        
        <view class="wrapper" :style="{ top: navHeight + navTop + 6 + 'px' }" v-if="list && list.length > 0">
            <view class="list_item" v-for="(item, index) in list" :key="index">
            	<image class="check-img" :src="item.checked == true ? '/static/icon/g_choose.png' : '/static/icon/g_choose2.png'" @click="checked(item)"></image>
            	<view class="item">
                    <image :src="item.image_uri" class="item_img" mode="aspectFit"></image>
                    <view class="right">
                        <view class="name">{{item.goods_name}}</view>
                        <view class="sub" v-if="item.goods_sku_name">规格：{{item.goods_sku_name}}</view>
                        <view class="right-down">
                            <view>
                                <view class="price">￥<text class="price-num">{{item.sale_price}}</text></view>
                            </view>
                            <u-number-box v-model="item.goods_nums" input-width="70" :disabled-input="true"
                                :index="index" :min="1" @change="valChange(index, $event)"></u-number-box>
                                 <!-- :max="item.stock" -->
                        </view>
                    </view>
                </view>
            </view>
        </view>
        <view class="none" v-if="list && list.length == 0" @click="$sTo('/pages/tabbar/index')">
            <view>您的购物车暂无商品</view>
            <view>去商城逛逛吧</view>
        </view>
        <!-- 底部 -->
        <view class="footer-box" v-if="list && list.length > 0">
            <view class="footer">
            	<view class="shop-list">
            		<view style="display: flex;align-items: center;">
            			<view style="display: flex;align-items: center;font-size: 26rpx;" @click="selectAll()">
                            <image class="check-img" :src="checkAll == true ? '/static/icon/g_choose.png' : '/static/icon/g_choose2.png'" ></image>
            				<view class='footer-center all'>全选</view>
            			</view>
            			<view class='footer-center' style="margin-left: 25rpx;font-weight: bold;">
            				合计:<view style="font-size: 34rpx;color: #333;font-weight: bold;">￥{{ allMoney.toFixed(2) }}</view>
            			</view>
            		</view>
            		<view class="all" @click="del(item)">删除</view>
            	</view>
            	<view class="qujiesuan2">
            		<view class="qujiesuan" @click="subOrder">去结算</view>
            	</view>
            </view>
        </view>
    </view>
</template>

<script>
    export default {
        data() {
            return {
                navHeight: 0,
                navTop: 0,
                
                checkAll: false, //全选
                list: [],
                sellectList: '',
                allMoney: 0, //价钱
                isShow: false,
            };
        },
        onLoad(option) {
            let _this = this;
            let menuButtonInfo = uni.getMenuButtonBoundingClientRect();
            _this.navHeight = menuButtonInfo.height;
            _this.navTop = menuButtonInfo.top;
        },
        onShow() {
            this.allMoney = 0
            this.checkAll = false
            if (this.$getSync('userToken')) {
                this.getList();
            }
        },
        methods: {
            //去结算
            subOrder() {
            	var idss = []
            	var goods = []
            	this.list.forEach((item, index) => {
            		if (item.checked == true) {
            			idss.push(item.id)
            			var obj = {
            				goods_id: item.goods_id, //商品id
            				goods_sku_id: '', //规格id
                            // goods_sku_name: item.goods_sku_name, //规格名称
                            goods_nums: item.goods_nums //商品数量
            			}
            			goods.push(obj)
            		}
            	})
            	if (idss == '') {
            		this.$toast("请选择商品后结算")
            		return
            	}
                
            	this.$ajax('confirmOrder', {
            		userToken: this.$getSync('userToken'),
                    buyType: 1, 
            		buy_id: idss.join(','), //购物车id	
            		goods: goods, //[{"goods_id":1,"goods_sku_id":1,"goods_sku_name":'',"goods_nums":1}]
                    user_address_id: '',
                    supplier_id: '',
            	}).then(ret => {
            		if (ret.status == 0) {
            			this.$gTo('/pages/settlement/settlement?data=' + JSON.stringify(ret.data) + '&buyId=' + idss.join(','))
            		} else {
            			this.$toast(ret.message)
            		}
            	})
            },
            
            //单选
            checked(item) {
                this.sellectList = [];
                item.checked = !item.checked;
                this.list.forEach((el, index) => {
                    if (el.checked) {
                        this.sellectList.push(el)
                    }
                })
                if (this.sellectList.length == this.list.length) {
                    this.checkAll = true
                } else {
                    this.checkAll = false
                }
                this.zongji()
            },
            
            
            //全选
            selectAll() {
                this.checkAll = !this.checkAll;
                if (this.checkAll) {
                    this.list.forEach((item, index) => {
                        item.checked = true
                    })
                } else {
                    this.list.forEach((item, index) => {
                        item.checked = false
                    })
                }
                this.zongji()
            },
            
            //修改数量
            valChange(index, e) {
            	this.list[index].goods_nums = e.value;
                this.$ajax('editCart', {
                    userToken: this.$getSync('userToken'),
                    id: this.list[index].id, //购物车id
                    goods_nums: e.value, //数量 0为删除
                }).then(ret => {
                    if (ret.status == 0) {
                        this.zongji();
                    } else {
                        this.$toast(ret.message);
                    }
                });
            },
            
            //总计
            zongji() {
            	var a = false;
            	this.allMoney = 0
            	this.list.forEach(item => {
            		if (!item.checked) {
            			a = true
            		} else {
                        this.allMoney += item.goods_nums * item.sale_price
            		}
            	})
            	if (!a) {
            		this.checkAll = true
            	} else {
            		this.checkAll = false
            	}
            },
            
            //删除商品
            del(item) {
                var ids = [];
                this.list.forEach((el) => {
                    if (el.checked) {
                        ids.push(el.id)
                    }
                })
                if (ids.length == 0) {
                    this.$toast('请选择商品');
                    return false;
                }
                this.$confirm('确定删除商品？',()=>{
                    this.$ajax('delCart', {
                        userToken: this.$getSync('userToken'),
                        id: ids.join(','), //购物车id
                    }).then(ret => {
                        if (ret.status == 0) {
                            this.$toast('删除成功');
                            setTimeout((res) => {
                            	this.getList();
                                this.checkAll = false;
                                this.allMoney = 0;
                            }, 500)
                        } else {
                            this.$toast(ret.message);
                        }
                    });
                })
            },
            
            //获取购物车列表
            getList() {
                this.$ajax('buycarList', {
                    userToken: this.$getSync('userToken'),
                    id: '',//购物车id
                }).then(ret => {
                    if (ret.status == 0) {
                        this.list = ret.data;
                        if (this.list.length > 0) {
                        	this.list.forEach(val => {
                        		this.$set(val, 'checked', false)
                        	})
                        }
                        // this.isShow = true;
                    } else {
                        this.$toast(ret.message);
                    }
                });
            },
            
        }
    };
</script>

<style lang="scss">
    .none{
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        font-size: 36rpx;
        color: #bfbfbf;
        margin: 130px auto 0;
    }
    .cart-icon{
        width: 530rpx;
        height: 332rpx;
        margin-bottom: 50rpx;
    }
    
    
    page{
        background: #f5f5f5;
        border-top: 2rpx solid #e6e6e6;
    }
    .header {
        position: fixed;
        width: 100%;
        top: 0px;
        z-index: 22;
        background-color: #ffffff;
        border-bottom: 2rpx solid #e6e6e6;
    }
    .header-logo {
        font-size: 32rpx;
        color: #000;
    }
    .wrapper{
        position: absolute;
        left: 0;
        padding-bottom: 300rpx;
    }
    .list_item {
    	width: 100vw;
    	border-bottom: 2rpx solid #e6e6e6;
    	padding: 36rpx 20rpx;
        display: flex;
    	justify-content: space-between;
    	align-items: center;
    	background-color: #FFFFFF;
    	box-sizing: border-box;
    }
    .check-img {
    	width: 31upx;
    	height: 31upx;
    }
    
    .item_img {
    	width: 22vw;
    	height: 22vw;
    	border-radius: 10rpx;
    	margin-right: 15rpx;
    	border: 2rpx solid #e3e3e3;
        overflow: hidden;
    }
    .right{
        width: 63vw;
        height: 22vw;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
    }
    .name{
        font-weight: bold;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }
    .sub{
        font-size: 24rpx;
        color: #6d6d6d;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }
    .item{
        display: flex;
        justify-content: space-between;
        align-items: center;
    }
    .right > view:first-child{
        font-size: 28rpx;
        font-weight: bold;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }
    .right-down{
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding-top: 15rpx;
    }
    .price{
        color: #f62a2a;
        font-size: 24rpx;
    }
    .price-num{
        font-size: 34rpx;
        font-weight: bold;
    }
    .price2{
        font-size: 24rpx;
        text-decoration: line-through;
        color: #808080;
    }
    
    
    //底部
    .footer-box{
        width: 100%;
        background-color: #FFFFFF;
        position: fixed;
        bottom: 0;
        z-index: 99;
    }
    .footer {
        width: 100%;
    	padding: 0 3vw;
    	box-sizing: border-box;
        display: flex;
        align-items: center;
        justify-content: space-between;
    }
    
    .shop-list {
    	padding: 0rpx 20 0 20rpx;
    	display: flex;
    	align-items: center;
    	justify-content: space-between;
    	height: 100rpx;
    	flex: 1;
    }
    
    .shop-list image {
    	width: 4vw;
    	height: 4vw;
    	display: block;
    }
    
    .footer-center {
    	margin-left: 20rpx;
    	height: 100%;
    	line-height: 95rpx;
    	display: flex;
    	align-items: center;
    	font-size: 28rpx;
        
    }
    
    .footer-center view {
    	margin-left: 20rpx;
    	font-weight: 500;
    	line-height: 95rpx;
    	display: flex;
    	align-items: center;
    }
    .all{
        color: #545454;
        font-size: 24rpx;
    }
    .qujiesuan {
    	font-size: 28rpx;
    	line-height: 80rpx;
    	text-align: center;
        color: #fff;
    	width: 180rpx;
    	height: 80rpx;
        background: linear-gradient(to right, #00c27d, #00734c);
        border-radius: 80rpx;
        margin-left: 25rpx;
    }
    .qujiesuan2 {
    	display: flex;
    	justify-content: space-around;
    	align-items: center;
    }

</style>
